# -*- coding: utf-8 -*-
from collections import UserList


def gen_func():
    yield 1


def fib(index):
    """
    斐波那契数列 0 1 1 2 3 5 8
    :param index:
    :return:
    """
    if index <= 2:
        return 1
    else:
        return fib(index - 1) + fib(index - 2)


def fib2(index):
    re_lst = []
    n, a, b = 0, 0, 1
    while n < index:
        re_lst.append(b)
        a, b = b, a + b
        n += 1
    return re_lst


def gen_fib(index):
    n, a, b = 0, 0, 1
    while n < index:
        yield b
        a, b = b, a + b
        n += 1


class MyList:
    def __iter__(self):
        i = 0
        try:
            while True:
                yield self[i]
                i += 1
        except IndexError:
            return


def my_read_lines(f, newline):
    buf = ''
    while True:
        while newline in buf:
            pos = buf.index(newline)
            yield buf[:pos]
            buf = buf[pos + len(newline):]
        chunk = f.read(1024)
        if not chunk:
            break
        buf += chunk


def my_read_line_test():
    with open('test.txt', 'r') as f:
        for line in my_read_lines(f, '{|}'):
            print(line)


if __name__ == '__main__':
    res = fib2(5)
    print(res)

    for data in gen_fib(10):
        print(data)
